package com.itheima.util;

import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class BallUtil {
    private static Random r= new Random();

    private  ArrayList<Integer> bluepool=new ArrayList<>(List.of(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33));
  /*  static {
        for (int i = 1; i <= 33; i++) {
            bluepool.add(i);
        }
    }*/
  private static int getRed(){
        return r.nextInt(16)+1;
    }
    private  ArrayList<Integer> getBlue(){
        ArrayList<Integer> blues = new ArrayList<>();
        for (int j=0;blues.size()<6;j++) {
                int i = r.nextInt(bluepool.size());
                Integer blue = bluepool.get(i);
                //判断是否四连
                if(is4lian(blues,blue)){
                    continue;
                }
                bluepool.remove(i);
                blues.add(blue);
        }
        return blues;
    }

    //判断是否四连
    //参数  球   现在已经生成的球的集合
    //返回值  是  不是
    private static boolean is4lian(ArrayList<Integer> list,int blue) {
        boolean contain_3s = list.contains(blue - 3);
        boolean contain_2s = list.contains(blue - 2);
        boolean contain_1s = list.contains(blue - 1);
        boolean contain1_s = list.contains(blue + 1);
        boolean contain2_s = list.contains(blue + 2);
        boolean contain3_s = list.contains(blue + 3);
        if(contain_3s&&contain_2s&&contain_1s){
            return true;
        }
        if(contain_2s&&contain_1s&&contain1_s){
            return true;
        }
        if(contain_1s&&contain1_s&&contain2_s){
            return true;
        }
        if(contain1_s&&contain2_s&&contain3_s){
            return true;
        }
        return false;
    }

    public static int[] getAll(){
        BallUtil ballUtil = new BallUtil();
        ArrayList<Integer> blues = ballUtil.getBlue();
        Collections.sort(blues);
        int red = ballUtil.getRed();
        int[] balls = new int[7];
        for (int i = 0; i < 6; i++) {
            balls[i]=blues.get(i);
        }
        balls[6]=red;
        return balls;
    }
}
